package com.ruoyi.procurementmanagement.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.procurementmanagement.domain.ErpWarehouseEntryProduct;
import com.ruoyi.procurementmanagement.service.IErpWarehouseEntryProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurementmanagement.mapper.ErpWarehouseEntryMapper;
import com.ruoyi.procurementmanagement.domain.ErpWarehouseEntry;
import com.ruoyi.procurementmanagement.service.IErpWarehouseEntryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
public class ErpWarehouseEntryServiceImpl implements IErpWarehouseEntryService
{
    @Autowired
    private ErpWarehouseEntryMapper erpWarehouseEntryMapper;

    @Autowired
    private IErpWarehouseEntryProductService warehouseEntryProductService;

    /**
     * 查询入库单
     *
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public ErpWarehouseEntry selectErpWarehouseEntryById(Long id)
    {
        return erpWarehouseEntryMapper.selectErpWarehouseEntryById(id);
    }

    /**
     * 查询入库单列表
     *
     * @param erpWarehouseEntry 入库单
     * @return 入库单
     */
    @Override
    public List<ErpWarehouseEntry> selectErpWarehouseEntryList(ErpWarehouseEntry erpWarehouseEntry)
    {
        return erpWarehouseEntryMapper.selectErpWarehouseEntryList(erpWarehouseEntry);
    }

    /**
     * 新增入库单
     *
     * @param erpWarehouseEntry 入库单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertErpWarehouseEntry(ErpWarehouseEntry erpWarehouseEntry) {
        // 新增入库单时，状态设置为未提交
        erpWarehouseEntry.setStatus("未提交");
        erpWarehouseEntry.setCreateTime(DateUtils.getNowDate());

        // 插入入库单主表
        int result = erpWarehouseEntryMapper.insertErpWarehouseEntry(erpWarehouseEntry);

        // 获取新插入的入库单ID
        Long entryId = erpWarehouseEntry.getId();

        // 插入入库单明细
        List<ErpWarehouseEntryProduct> entryProducts = erpWarehouseEntry.getEntryProducts();
        if (entryProducts != null && !entryProducts.isEmpty()) {
            for (ErpWarehouseEntryProduct product : entryProducts) {
                product.setWarehouseEntryId(entryId);
                warehouseEntryProductService.insertErpWarehouseEntryProduct(product);
            }
        }

        return result;
    }

    /**
     * 修改入库单
     *
     * @param erpWarehouseEntry 入库单
     * @return 结果
     */
    @Override
    public int updateErpWarehouseEntry(ErpWarehouseEntry erpWarehouseEntry) {
        erpWarehouseEntry.setUpdateTime(DateUtils.getNowDate());

        // If status is being updated to "已结算", set the settlement flag
        if ("已结算".equals(erpWarehouseEntry.getStatus())) {
            erpWarehouseEntry.setSupplierSettlementFlag(1);
        }

        return erpWarehouseEntryMapper.updateErpWarehouseEntry(erpWarehouseEntry);
    }

    /**
     * 批量删除入库单
     *
     * @param ids 需要删除的入库单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteErpWarehouseEntryByIds(Long[] ids) {
        // 先删除关联的入库单商品明细
        for (Long id : ids) {
            List<ErpWarehouseEntryProduct> entryProducts = warehouseEntryProductService.selectErpWarehouseEntryProductList
                    (new ErpWarehouseEntryProduct().setWarehouseEntryId(id));
            if (!entryProducts.isEmpty()) {
                Long[] productIds = entryProducts.stream().map(ErpWarehouseEntryProduct::getId).toArray(Long[]::new);
                warehouseEntryProductService.deleteErpWarehouseEntryProductByIds(productIds);
            }
        }
        // 再删除入库单
        return erpWarehouseEntryMapper.deleteErpWarehouseEntryByIds(ids);
    }

    /**
     * 删除入库单信息
     *
     * @param id 入库单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteErpWarehouseEntryById(Long id) {
        // 先删除关联的入库单商品明细
        List<ErpWarehouseEntryProduct> entryProducts = warehouseEntryProductService.selectErpWarehouseEntryProductList(
                new ErpWarehouseEntryProduct().setWarehouseEntryId(id));
        if (!entryProducts.isEmpty()) {
            Long[] productIds = entryProducts.stream().map(ErpWarehouseEntryProduct::getId).toArray(Long[]::new);
            warehouseEntryProductService.deleteErpWarehouseEntryProductByIds(productIds);
        }
        // 再删除入库单
        return erpWarehouseEntryMapper.deleteErpWarehouseEntryById(id);
    }
}
